<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\services\crud;

use app\dao\crud\QuestionCategoryDao;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;

/**
 * 题目分类业务逻辑层
 * Class QuestionCategoryServices
 * @package app\services\crud
 */
class QuestionCategoryServices extends BaseServices
{
    /**
     * QuestionCategoryServices constructor.
     * @param QuestionCategoryDao $dao
     */
    public function __construct(QuestionCategoryDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 获取分类列表
     * @param array $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(array $where = [])
    {
        $where['is_del'] = 0;
        $list = $this->dao->getCategoryList($where);
        return $list;
    }

    /**
     * 获取树形分类列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTreeList()
    {
        return $this->dao->getCategoryTree();
    }

    /**
     * 创建分类
     * @param array $data
     * @return mixed
     */
    public function create(array $data)
    {
        $categoryData = [
            'name' => $data['name'],
            'pid' => $data['pid'] ?? 0,
            'sort' => $data['sort'] ?? 0,
            'status' => $data['status'] ?? 1,
        ];

        $category = $this->dao->save($categoryData);
        if (!$category) {
            throw new AdminException('创建分类失败');
        }

        return $category->id;
    }

    /**
     * 更新分类
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function update(int $id, array $data)
    {
        $category = $this->dao->get($id);
        if (!$category) {
            throw new AdminException('分类不存在');
        }

        // 不能设置自己为父级
        if (isset($data['pid']) && $data['pid'] == $id) {
            throw new AdminException('不能设置自己为父级分类');
        }

        $updateData = [];
        $allowFields = ['name', 'pid', 'sort', 'status'];
        foreach ($allowFields as $field) {
            if (isset($data[$field])) {
                $updateData[$field] = $data[$field];
            }
        }

        return $this->dao->update($id, $updateData) ? true : false;
    }

    /**
     * 删除分类
     * @param int $id
     * @return bool
     */
    public function delete(int $id)
    {
        $category = $this->dao->get($id);
        if (!$category) {
            throw new AdminException('分类不存在');
        }

        // 检查是否有子分类
        $hasChildren = $this->dao->count(['pid' => $id, 'is_del' => 0]);
        if ($hasChildren > 0) {
            throw new AdminException('该分类下有子分类，无法删除');
        }

        // 软删除
        return $this->dao->update($id, ['is_del' => 1]) ? true : false;
    }
}

